package com.tieshan.smartwindow.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.media.MediaPlayer
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.widget.Toast
import com.tieshan.smartwindow.R
import com.tieshan.smartwindow.app.AppActivity
import com.tieshan.smartwindow.bean.FileNameBean
import com.tieshan.smartwindow.other.ALPermissionManager
import com.tieshan.smartwindow.other.FileConfig
import com.tieshan.smartwindow.other.FileUtil
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_window.*
import kotlinx.android.synthetic.main.activity_window.iv_home
import java.io.File
import java.util.*


/**
 * author : Hleo
 * time   : 2022/2/9
 * desc   : 窗口显示
 */
class WindowActivity : AppActivity() {
    var indexleft=0
    var indexright=0
    var mHandler: Handler? = null
    //按键隐藏事件
    var mBtnHandler: Handler? = null
    var timeIndex=0

    override fun getLayoutId(): Int {
        return R.layout.activity_window
    }

    override fun initView() {
        mHandler = Handler(Looper.getMainLooper())
        mHandler!!.postDelayed(mRunner, 1000L)
        mBtnHandler = Handler(Looper.getMainLooper())
        mBtnHandler!!.postDelayed(mBtnRunner, 3000L)
        bt_1.setOnClickListener {
            showView()
            indexleft++
            if (indexleft==5&&indexright==5)
                startActivity(DealActivitity::class.java)

        }
        bt_2.setOnClickListener {
            showView()

        }
        bt_3.setOnClickListener {
            indexright++
            if (indexleft==5&&indexright==5)
                startActivity(DealActivitity::class.java)
            showView()
        }
        bt_4.setOnClickListener {
            ALPermissionManager.reBestartWlan()
            showView()

        }
        bt_5.setOnClickListener {
            ALPermissionManager.setIp()
            showView()

        }
        bt_6.setOnClickListener {
            showView()
        }

        iv_home.setOnClickListener {
            finish()
        }
    }

    //点击以后图标显示
    private fun showView() {
        iv_home.visibility= View.VISIBLE
        mHandler?.removeCallbacks(mBtnRunner)
        mHandler!!.postDelayed(mBtnRunner, 3000L)
    }


    var mRunner: Runnable = object : Runnable {
        override fun run() {
            timeIndex++
            if (timeIndex>=5){
                //如果操作时间间隔大于5秒，则清零，重新计算
                indexleft=0
                indexright=0
                timeIndex=0
            }
            mHandler!!.postDelayed(this, 1000)
        }
    }

    var alphaAnimation: AlphaAnimation?= AlphaAnimation(1.0f,0.0f)
    private fun initAnim() {
        iv_home.visibility= View.INVISIBLE
        alphaAnimation!!.duration = 1000
        alphaAnimation!!.fillBefore = true
        alphaAnimation!!.interpolator = LinearInterpolator()
        iv_home.setAnimation(alphaAnimation)
        alphaAnimation!!.start()
    }

    var mBtnRunner: Runnable = object : Runnable {
        override fun run() {
            initAnim()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        mHandler!!.removeCallbacks(mRunner)
    }


    @SuppressLint("ResourceType")
    fun start() {


    }

    @SuppressLint("ClickableViewAccessibility")
    override fun initData() {

    }
}